package com.dcy.znoa.service.publiz.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.companyAddress.SelectCompanyAddressModel;
import com.dcy.znoa.dao.publiz.CompanyAddressMapper;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.publiz.CompanyAddress;
import com.dcy.znoa.service.publiz.CompanyAddressService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 企业地址表：目前用于与物业交互时使用，只有一个是默认的地址。 服务实现类
 * </p>
 *
 * @author cook
 * @since 2018-05-24
 */
@Service
public class CompanyAddressServiceImpl implements CompanyAddressService {

    @Autowired
    CompanyAddressMapper companyAddressMapper;

    @Override
    public JsonForList selectList(Oper oper, SelectCompanyAddressModel model) {
        EntityWrapper ew = new EntityWrapper(new CompanyAddress());
        if(model.getCompId()!=null){
            ew.eq("comp_id",model.getCompId());
        }
        if(model.getIsDefault()!=null){
            ew.eq("is_default",model.getIsDefault());
        }
        Page page = new Page(model.getPageNum(), model.getPageSize());
        List<CompanyAddress> list;
        list = companyAddressMapper.selectPage(page, ew);
        return new JsonForList(list, page.getTotal());
    }

    @Override
    public int insert(CompanyAddress cp) {
        return  companyAddressMapper.insert(cp);
    }

    @Override
    public int updateById(CompanyAddress cp) throws InvocationTargetException, IllegalAccessException {
        CompanyAddress companyAddress = companyAddressMapper.selectById(cp.getCaId());
        BeanUtils.copyProperties(companyAddress, cp);
        return companyAddressMapper.updateById(companyAddress);
    }

    @Override
    public int delById(Long caId) {
        return companyAddressMapper.deleteById(caId);
    }


}
